{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "04230.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/04230.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_CcAEOMQZbBG",
        "colab_type": "text"
      },
      "source": [
        "## 3.10 多层感知机的简洁实现"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I20I0FQwbTDk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# !git clone https://github.com/ShusenTang/Dive-into-DL-PyTorch.git"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GwQt2w8CjMQf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "from torch import nn \n",
        "from torch.nn import init \n",
        "import numpy as np\n",
        "import sys \n",
        "sys.path.append('Dive-into-DL-PyTorch/code')\n",
        "import d2lzh_pytorch as d2l "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tgLJVOcH1M-c",
        "colab_type": "text"
      },
      "source": [
        "### 3.10.1 定义模型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "04jZK_jv1TKS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "num_inputs, num_outputs, num_hiddens = 784, 10, 256 \n",
        "\n",
        "net = nn.Sequential(\n",
        "    d2l.FlattenLayer(), \n",
        "    nn.Linear(num_inputs, num_hiddens), \n",
        "    nn.ReLU(), \n",
        "    nn.Linear(num_hiddens, num_outputs), \n",
        ")\n",
        "\n",
        "for params in net.parameters():\n",
        "    init.normal_(params, mean=0, std=0.01)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SIGyV9cI10kW",
        "colab_type": "text"
      },
      "source": [
        "### 3.10.2 读取数据并训练模型"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ifsPG6Fd17w4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "968a14d6-d457-4835-f76d-d0455ff6abd1"
      },
      "source": [
        "# 这个 loss 有问题，想办法改过来 \n",
        "batch_size = 256 \n",
        "train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)\n",
        "\n",
        "loss = torch.nn.CrossEntropyLoss()\n",
        "optimizer = torch.optim.SGD(net.parameters(), lr=0.5)\n",
        "\n",
        "num_epochs = 5 \n",
        "d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)\n"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "epoch 1, loss 0.0031, train acc 0.706, test acc 0.778\n",
            "epoch 2, loss 0.0019, train acc 0.819, test acc 0.827\n",
            "epoch 3, loss 0.0017, train acc 0.844, test acc 0.833\n",
            "epoch 4, loss 0.0015, train acc 0.857, test acc 0.849\n",
            "epoch 5, loss 0.0014, train acc 0.865, test acc 0.839\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}